const findDisappearedNumbers = (nums) => {
  let i = 0;
  while (i < nums.length) {
    if (nums[i] === i + 1) {
      // 当前数字出现在它理应出现在位置，则不用替换
      i++;
      continue;
    }
    const idealIdx = nums[i] - 1;
    if (nums[i] === nums[idealIdx]) {
      // 当前数字理应出现的位置已经有数字存在
      i++;
      continue;
    }
    [nums[idealIdx], nums[i]] = [nums[i], nums[idealIdx]];
  }
  console.log(nums);
  const res = [];
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] !== i + 1) {
      // 当前
      res.push(i + 1);
    }
  }
  return res;
};

const nums = [4, 3, 2, 7, 8, 2, 3, 1];
findDisappearedNumbers(nums);
